package com.service;

import com.meta.HtMetaInfoSyncServiceGrpc;
import com.meta.HtMetaSyncRequest;
import com.meta.HtMetaSyncResponse;
import com.meta.SyncType;
import io.grpc.StatusRuntimeException;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;

/**
 * @author xiaoshu
 */
@Service
public class HtMetaInfoSyncClient {

    @GrpcClient("local-grpc-server")
    private HtMetaInfoSyncServiceGrpc.HtMetaInfoSyncServiceBlockingStub stub;


    public String syncMeta(final String json,final SyncType syncType) {
        try {
            HtMetaSyncResponse htMetaSyncResponse = stub.syncMeta((
                    HtMetaSyncRequest.newBuilder()
                            .setSyncJson(json)
                            .setSyncType(syncType)
                            .build()));
            String code = htMetaSyncResponse.getCode();
            String msg = htMetaSyncResponse.getMsg();
            return code+":"+msg;
        } catch (final StatusRuntimeException e) {
            return "FAILED with " + e.getStatus().getCode().name();
        }
    }

}